﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Elysium.Demo;
using Elysium.Demo.Configuracion;
using Elysium.Demo.Ventas;
using Elysium.Demo.Compras;
using Elysium.Demo.CapitalHumano;
using Elysium.Demo.Inventarios;
using Elysium.Demo.Bancos;
using Elysium.Demo.PuntoVenta;
using Elysium.Demo.CRM;
using Elysium.Demo.MiPerfil;
using SIIE_ERP.Codigo;
using System.Data;
using SIIE_ERP.BaseDatos.Configuracion;
using Elysium.Demo.Configuracion.PagoLicencias;

namespace Elysium.Demo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow_NoLicencia : Elysium.Controls.Window
    {
        UsuarioActivo _sesion;
        System.Timers.Timer _t;

        public MainWindow_NoLicencia(UsuarioActivo sesion)
        {
            InitializeComponent();
            _sesion = sesion;
            _t = new System.Timers.Timer();
            _t.AutoReset = false;
            _t.Elapsed += new System.Timers.ElapsedEventHandler(ValidarSesion);
            _t.Interval = 60000; // Verificar si la sesion esta activa cada minuto
            _t.Start();
            LabelHola.Content += " " + sesion.UserName + "!";

            dbo_Sec_MenuUser MenuUser = new dbo_Sec_MenuUser();
          //  DataTable RFCs = MenuUser.Get_RFCs_by_Id(sesion.IdUsuario);

        //    _sesion.RFC = RFCs.Rows[0]["RFC"].ToString();
           Usuarios_Consulta usuarioConsulta = new Usuarios_Consulta(ref _sesion);
           usuarioConsulta.ButtonInformacion.Visibility = System.Windows.Visibility.Hidden;

           usuarioConsulta.ButtonNuevo.Visibility = System.Windows.Visibility.Hidden;
           usuarioConsulta.ButtonRegresar.Visibility = System.Windows.Visibility.Hidden;
            Grid_Elements.Child = usuarioConsulta;
           
        }

        public UsuarioActivo Sesion { get { return this._sesion; } }

        private void ValidarSesion(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (!_sesion.ChecarFecha())
            {
                // Ocultamos primero la ventana para que ya no pueda seguir haciendo cosas
                this.Dispatcher.Invoke(new System.Action(() =>
                {
                    this.Hide();
                }), null);      
          
                MessageBox.Show("La sesión expiró");
                
                // Cerramos la ventana
                this.Dispatcher.Invoke(new System.Action(() =>
                {
                    this.Close();
                }), null);                
            }
            _t.Start();
        }

        

        private void Window_SizeChanged_1(object sender, SizeChangedEventArgs e)
        {

        }

        private void Window_LayoutUpdated_1(object sender, EventArgs e)
        {

        }

        

        private void Window_Closed(object sender, EventArgs e)
        {
            Application.Current.Shutdown(0); // Cerrar definitivamente la aplicacion
        }        

        private void ButtonCerrarSesion_Click(object sender, RoutedEventArgs e)
        {
            Login.Login Login = new Login.Login();
            Login.Show();
            this.Hide();  
        }

        private void Window_GotFocus(object sender, RoutedEventArgs e)
        {

        }

        private void ButtonLicencias_Click_1(object sender, RoutedEventArgs e)
        {
            Grid_Elements.Child = null;
            Grid_Elements.Child = new CompraLicencias(ref _sesion);
        }

        private void ButtonPago_Click_1(object sender, RoutedEventArgs e)
        {
            Grid_Elements.Child = null;
            Grid_Elements.Child = new DatosBancarios(ref _sesion);
        }
    }
}
